﻿import sys
import Hobot.GPIO as GPIO
import time
import random

# 初始化全局变量
tri_gpio = 11

outer_control_angle = 0;

def setup():

    # 从命令行参数中读取并转换为整数
    global outer_control_angle
    global tri_gpio
    if(sys.argv[1]):
        outer_control_angle = int(sys.argv[1])
    if(sys.argv[2]):
        tri_gpio = int(sys.argv[2])
    



    GPIO.cleanup()  # 清理之前的配置
    GPIO.setmode(GPIO.BOARD)  # 设置GPIO模式

    GPIO.setup(tri_gpio, GPIO.OUT, initial=GPIO.LOW)





# state = True

loopCount = 0;

def loop():
    global loopCount
    while True:

        # state = not state
        # angle = 90 if state else 0

        angle = outer_control_angle

        angle_time = 0.5+2*(angle/180)

  
        # 设置高电平
        GPIO.output(tri_gpio, GPIO.HIGH)
        # 设置休眠  毫秒
        time.sleep(angle_time/1000)
        # 设置低电平
        GPIO.output(tri_gpio, GPIO.LOW)

        # 等待1秒
        time.sleep(0.05)
        loopCount+=1
        if loopCount>3:
            print("执行完毕转向角度：{0}".format(angle))
            break


def destroy():
    GPIO.cleanup()

if __name__ == '__main__':
    setup()
    try:
        loop()
        
    except KeyboardInterrupt:
        destroy()
    finally:
        destroy()
